import 'package:casdon/core/network/i_api_client.dart';
import 'package:casdon/modules/feedback/data/api/feedback_reply_api.dart';
import 'package:casdon/modules/feedback/data/api/feedback_type_enum_api.dart';
import 'package:casdon/modules/feedback/data/api/submit_feedback_api.dart';
import 'package:casdon/modules/feedback/data/model/feedback_dialogue_model.dart';
import 'package:casdon/modules/feedback/data/model/feedback_record_model.dart';
import 'package:casdon/modules/feedback/data/repository/feedback_repository.dart';

class FeedbackRepositoryImpl extends FeedbackRepository {
  // 字段类型也是抽象接口
  final IApiClient _apiClient;

  FeedbackRepositoryImpl(this._apiClient);

  @override
  Future<List<ResponseFeedbackTypeEnum>> getFeedbackTypeList() async {
    final res = await _apiClient.getTyped<Map<String, dynamic>>(
      '/client-operation/feedback/getFeedBackTypeEnum',
      fromJsonT: (json) => Map<String, dynamic>.from(json as Map),
    );
    final data = res?.data ?? <String, dynamic>{};
    return data.entries
        .map(
          (entry) => ResponseFeedbackTypeEnum.fromEntry(entry.key, entry.value),
        )
        .toList();
  }

  @override
  Future<void> submitFeedback(RequestSubmitFeedbackApi request) async {
    await _apiClient.post<void>(
      '/client-operation/feedback/submitFeedback',
      data: request.toJson(),
    );
  }

  @override
  Future<List<FeedbackRecordModel>> getFeedbackList() async {
    final res = await _apiClient.getTyped<List<FeedbackRecordModel>>(
      '/client-operation/feedback/getFeedbackInfoList',
      fromJsonT: (json) {
        if (json is List) {
          return json
              .whereType<Map<String, dynamic>>()
              .map(FeedbackRecordModel.fromJson)
              .toList();
        }
        if (json is Map<String, dynamic>) {
          // 部分接口可能将列表包裹在 data 字段
          final list = json['list'] as List<dynamic>? ?? [];
          return list
              .whereType<Map<String, dynamic>>()
              .map(FeedbackRecordModel.fromJson)
              .toList();
        }
        return <FeedbackRecordModel>[];
      },
    );
    return res?.data ?? <FeedbackRecordModel>[];
  }

  @override
  Future<List<FeedbackDialogueModel>> getFeedbackDialogues(
    String feedbackInfoId,
  ) async {
    final res = await _apiClient.getTyped<List<FeedbackDialogueModel>>(
      '/client-operation/feedback/getFeedbackDialogueList/$feedbackInfoId',
      fromJsonT: (json) {
        if (json is List) {
          return json
              .whereType<Map<String, dynamic>>()
              .map(FeedbackDialogueModel.fromJson)
              .toList();
        }
        return <FeedbackDialogueModel>[];
      },
    );
    return res?.data ?? <FeedbackDialogueModel>[];
  }

  @override
  Future<void> finishFeedback(String feedbackInfoId) async {
    await _apiClient.post<void>(
      '/client-user/feedback/finishFeedBack',
      data: {'id': feedbackInfoId},
    );
  }

  @override
  Future<void> replyFeedback(RequestFeedbackReplyApi request) async {
    await _apiClient.post<void>(
      '/client-user/feedback/appUsrReply',
      data: request.toJson(),
    );
  }
}
